<?php
/**
 * The request class.
 */
class Request {
    /**
     * The site's base URL.
     * @var string
     */
	protected $baseUrl = '';
	/**
	 * The requested URI without the base URL.
	 * @var string
	 */
	protected $requestUri = '';
    /**
     * The page to be rendered for this run.
     * @var string
     */
	protected $page = '';
    /**
     * The method to call for this run.
     * @var string
     */
	protected $method = '';
    /**
     * The parameters determined from the URI.
     * @var array
     */
	protected $params = array();
	protected $route = null;

    /**
     * Construct the object by breaking down the URI.
     */
	public function __construct() {
	    $this->page = Framework::getConfig()->getSection('general')->page;
	    $this->method = Framework::getConfig()->getSection('general')->method;
	    $parts = explode('/', $_SERVER['SCRIPT_NAME']);
		array_pop($parts);
		$this->baseUrl = implode('/', $parts);
		$this->setParams($_REQUEST);

		$this->requestUri = str_replace($this->baseUrl, '', $_SERVER['REQUEST_URI']);
	}

    /**
     * Set the base URL. This is determined by the constructor but can be overridden here.
     * @see Request::__construct()
     * @param string $url
     */
	public function setBaseUrl($url) {
		if(is_string($url)) {
			$this->baseUrl = $url;
		}
	}

    /**
     * Get the base URL.
     * @return string
     */
	public function getBaseUrl() {
		return $this->baseUrl;
	}
	
	/**
	 * Get the requested URI.
	 * @return string
	 */
	public function getRequestUri() {
	    return $this->requestUri;
	}

    /**
     * Set the page name. This is determined by the constructor but can be overridden here.
     * @see Request::__construct()
     * @param string $name
     */
	public function setPageName($name) {
		if(is_string($name)) {
			$this->page = $name;
		}
	}

    /**
     * Get the page name.
     * @return string
     */
	public function getPageName() {
		return $this->page;
	}

    /**
     * Set the method name. This is determined by the constructor but can be overridden here.
     * @see Request::__construct()
     * @param string $name
     */
	public function setMethodName($name) {
		if(is_string($name)) {
			$this->method = $name;
		}
	}

    /**
     * Get the method name.
     * @return string
     */
	public function getMethodName() {
		return $this->method;
	}

    /**
     * Set the params. These are determined by the constructor but can be overridden here.
     * @see Request::__construct()
     * @param array $params
     */
	protected function setParams($params) {
		if(is_array($params)) {
			$this->params = $params;
		}
	}

    /**
     * Get the parameters.
     * @return array
     */
	public function getParams() {
		return $this->params;
	}

    /**
     * Get a single parameter by name optionally giving a default value.
     * @param string $param
     * @param mixed $default
     */
	public function getParam($param, $default = null) {
		if(array_key_exists($param, $this->params)) {
			return $this->params[$param];
		}
		return $default;
	}
	
	/**
	 * Use a route object to set some parameters.
	 * @param Route $route
	 */
	public function setRoute(Route $route) {
	    $this->route = $route;
	    $this->params += $route->getParams();
	}
}
